{{ extend './layout/main.html' }}

{{ block 'content' }}
<div class="container">
    <div class="page-header">
        <h1><a href="/">我的音乐</a><small>--编辑音乐</small>
            <h1>
    </div>
</div>
<div class="container">
    <form id="form" method="post" action="">
        <input type="hidden" name="id" class="form-control" value="{{ music.id }}">
        <div class="form-group">
            <label for="">歌曲标题</label>
            <input type="text" name="title" class="form-control" value="{{ music.title }}" placeholder="请输入歌曲标题">
        </div>
        <div class="form-group">
            <label for="">歌曲时长</label>
            <input type="text" name="time" class="form-control" value="{{ music.time }}" placeholder="请输入歌曲时长">
        </div>
        <div class="form-group">
            <label for="">歌手</label>
            <input type="text" name="singer" class="form-control" value="{{ music.singer }}" placeholder="请输入歌手姓名">
        </div>
        <div class="form-group">
            <label for="">歌曲文件</label>
            <input type="file" name="file"  value="{{ music.file }}">
            <p class="help-block">请上传歌曲文件.</p>
        </div>
        <div class="form-group">
            <label for="">歌词文件</label>
            <input type="file" name="filelrc"  value="{{ music.filelrc }}">
            <p class="help-block">请上传歌曲文件.</p>
        </div>
        <button type="submit" class="btn btn-success">完成编辑</button>

    </form>
</div>

<script type="text/javascript">
    $('#form').on('submit', function(e) {
        e.preventDefault();
        var inputs = document.querySelectorAll('input');

        var formData = new FormData();
        formData.append('id', inputs[0].value);
        formData.append('title', inputs[1].value);
        formData.append('time', inputs[2].value);
        formData.append('singer', inputs[3].value);

        formData.append('file', inputs[4].files[0] || inputs[4].defaultValue);
        formData.append('filelrc', inputs[5].files[0] || inputs[5].defaultValue);

        $.ajax({
            url: '/music/update-music',
            type: 'put',
            data: formData,
            contentType: false,
            processData: false,
            success: function(data) {
                alert(data.msg);
                if (data.code === '001') {
                    window.location = '/music/index';
                }
            }
        })
    })
</script>
{{ /block }}
